﻿using System.Web;
using System.Web.Security;
using ReadyApps.Common;

namespace ReadyApps.Wiki
{
    class WikiRoleManager : IWikiRoleManager
    {
        #region Variables and Constants

        public const string AUTHENTICATED_USERS = "*";
        public const string ANONYMOUS_USERS = "?";

        #endregion

        #region Properties

        public bool IsUserAuthenticated
        {
            get { return HttpContext.Current.User.Identity.IsAuthenticated; }
        }

        #endregion

        #region Public Methods

        public bool IsUserInRole(string username, string role)
        {
            TraceManager.BeginMethod(TraceComponent.RoleManager, "IsUserInRole");

            TraceManager.ParameterValue(TraceComponent.RoleManager, "role", role);
            bool userIsInRole = false;
            if (role == ANONYMOUS_USERS)
            {
                userIsInRole = true;
            }
            else if (role == AUTHENTICATED_USERS)
            {
                userIsInRole = HttpContext.Current.User.Identity.IsAuthenticated;
            }
            else
            {
                userIsInRole = Roles.IsUserInRole(username, role);
            }

            TraceManager.EndMethod(TraceComponent.RoleManager, "IsUserInRole");
            return userIsInRole;
        }

        #endregion
    }
}
